/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bettosimport.controllers;

import com.bettosimport.entidades.Usuario;
import com.bettosimport.negocio.UsuarioNeg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author Neil
 */
@Controller
@SessionAttributes({"user","detCompra"})
public class AccesoController {

    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/acceso", method = RequestMethod.POST)
    public String acceso(@ModelAttribute("Usuario") Usuario u, Model model) throws Exception {
        Object[] o = UsuarioNeg.Instancia().validarAcceso(u.getUsuario(), u.getClave());
        if (o == null) {
            return "login";
        } else {
            model.addAttribute("user", o);
            return "redirect:intranet";
        }
    }

    @RequestMapping(value = "/intranet", method = RequestMethod.GET)
    public String intranet() {
        return "intranet";
    }

    @RequestMapping(value = "/cerrarsesion", method = RequestMethod.GET)
    public String cerrarsesion(SessionStatus status, ModelMap modelo) {
        status.setComplete();
        modelo.remove("user");
        return "login";
    }

}
